package com.luckmgr.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.common.constant.SystemConstant;
import com.common.entity.PageBean;
import com.common.utils.StringUtils;
import com.luckmgr.entity.db.Activity;
import com.luckmgr.entity.db.Prize;
import com.luckmgr.entity.db.SunPrize;
import com.luckmgr.entity.db.User;
import com.luckmgr.service.ActivityService;
import com.luckmgr.service.LevelPrizeService;
import com.luckmgr.service.PrizeService;

@Controller
@RequestMapping("/prize")
public class PrizeController {
	
	@Autowired
	PrizeService prizeServiceImpl;
	@Autowired
	ActivityService activityServiceImpl;
	@Autowired
	LevelPrizeService levelPrizeService;
	
	@RequestMapping(value = "")
	public String prizemain(Model model,HttpSession session,Integer aid,Integer page) {
		if(null == page)
			page = 1;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("aid", aid);
		PageBean<Prize> pagebean = prizeServiceImpl.selectListPage(map, page);
		
		List<SunPrize> sunPrizes =levelPrizeService.selectSunByAid(aid);
		
		Map <Integer,SunPrize> sunPrizemap = new HashMap<Integer,SunPrize>();
		if(null != sunPrizes){
			for(SunPrize sunPrize : sunPrizes){
				sunPrizemap.put(sunPrize.getPrizeId(), sunPrize);
			}
		}
		if(null != pagebean.getData()){
			for(Prize prize :pagebean.getData()){
				SunPrize sunPrize =  sunPrizemap.get(prize.getId());
				if(null != sunPrize){
					prize.setTotal(sunPrize.getTotal());
					prize.setUsed(sunPrize.getUsed());
				}
			}
		}
		
		List<Activity> activitys = activityServiceImpl.selectList(null);
		
		model.addAttribute("pagebean", pagebean);
		model.addAttribute("activitys", activitys);
		model.addAttribute("aid", aid);
		return "prize/index";
	}
	
	@RequestMapping(value = "addup")
	public String addup(Model model,Integer id) {
		Prize prize = null;
		if(null !=id){
			prize = prizeServiceImpl.selectByPrimaryKey(id);
			model.addAttribute("entity", prize);
		}
		List<Activity> activitys = activityServiceImpl.selectList(null);
		model.addAttribute("activitys", activitys);
		
		return "prize/addup";
		
	}

	
	@RequestMapping(value = "delete")
	public String comdelete(Model model,@RequestParam(required = false)Integer id) {
		if(null != id && 0 != id){
			
			Prize prize  = prizeServiceImpl.selectByPrimaryKey(id);
			if(null == prize){
				model.addAttribute("msg", "奖品不存在！");
				return "prize/addup";
			}
			
			Activity activity = activityServiceImpl.selectByPrimaryKey(prize.getActivityId());
			if(null !=activity && activity.getStatus()==1){
				model.addAttribute("msg", "活动已开始，不能进行此操作!");
				return "prize/addup";
			}
			
			
			prizeServiceImpl.deletePrize(id);
		}
		return "redirect:/prize";
	}

	
	
	@RequestMapping(value = "update")
	public String update(Model model,HttpSession session,@ModelAttribute("entity") Prize prize) {
		User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
		if(null == prize.getPrizeIndex() 
				|| null == prize.getActivityId()){
			model.addAttribute("msg", "数据不完整请重新录入！");
			return "prize/addup";
		}
		
		Activity activity = activityServiceImpl.selectByPrimaryKey(prize.getActivityId());
		if(null ==activity || 1==activity.getStatus()){
			model.addAttribute("msg", "活动已开始或已删除，不能进行此操作!");
			return "prize/addup";
		}
		
		prize.setActivityName(activity.getName());
		prize.setOperatorName(user.getUserName());
		prize.setOperatorIp(user.getLoginIp());
		
		if(prize.getId()!=null && 0!=prize.getId()){
			prizeServiceImpl.updateEntity(prize);
		}else{
			
			if(StringUtils.isNull(prize.getPrizeName()) ){
				model.addAttribute("msg", "数据不完整请重新录入！");
				return "prize/addup";
			}
			
			prizeServiceImpl.addEntity(prize);
		}
		return "redirect:/prize";
	}
	
	
	
	
	
}
